package com.haoshuang.zhouzhoubang.ui.activity

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityBalanceWithdrawalDetailBinding
import com.haoshuang.zhouzhoubang.model.bean.WithdrawalDetailsBean
import com.haoshuang.zhouzhoubang.ui.adapter.BalanceWithdrawalDetailAdapter
import com.haoshuang.zhouzhoubang.ui.vm.BalanceWithdrawalDetailViewModel
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setupData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.vertical


/**
 *  提现明细
 */
class BalanceWithdrawalDetailActivity :
    BaseVMActivity<BalanceWithdrawalDetailViewModel, ActivityBalanceWithdrawalDetailBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<BalanceWithdrawalDetailActivity>()
        }
    }

    private val data = arrayListOf<WithdrawalDetailsBean>()
    private val adapter = BalanceWithdrawalDetailAdapter(data)

    override fun initView(savedInstanceState: Bundle?) {

        binding.titleBar.title = "提现明细"
        binding.titleBar.leftImageView().click { finish() }

        binding.refreshLayout.setOnRefreshListener {
            isShowing = false
            p = 1
            vm.getWithdrawalDetail("page" to p)
        }

        binding.refreshLayout.setOnLoadMoreListener {
            isShowing = false
            p++
            vm.getWithdrawalDetail("page" to p)
        }

        binding.recyclerView.vertical()
            .adapter = adapter

        adapter.setEmptyData()

    }

    override fun initData() {

        p = 1
        vm.getWithdrawalDetail("page" to p)

    }

    override fun startObserve() {

        vm.getWithdrawalDetailData.observeState(this){
            onLoading {showLoading()}
            onError {code, msg ->
                dismissLoading()
                toast(msg)
                binding.refreshLayout.setupData(emptyList(), data, adapter, p)
            }
            onSuccess {
                dismissLoading()
                binding.refreshLayout.setupData(it, data, adapter, p)
            }
        }

    }

}